//
//  WalletViewController.m
//  jingdongfang
//
//  Created by 郝志宇 on 16/8/11.
//  Copyright © 2016年 ZhiYu Hao. All rights reserved.
//

#import "WalletViewController.h"
#import "BillViewController.h"
#import "GetBalanceCashViewController.h"
#import "BalanceViewController.h"
#import "IGWebViewController.h"
#import "IGWalletApi.h"

@interface WalletViewController () <UITableViewDelegate, UITableViewDataSource> {
    UIImageView *imgView;
    UILabel *yue;
    UILabel *yidian;
}

@property (weak, nonatomic) IBOutlet UITableView *table;
@property(nonatomic,strong)IGWalletApi *walletApi;

@end

@implementation WalletViewController
#pragma mark -生命周期
- (void)viewDidLoad {
    [super viewDidLoad];

//    self.title = @"钱包";
    self.title = NSLocalizedString(@"Wallet", nil);
 
    self.view.frame = CGRectMake(0, -64, KSCREENWIDTH, KSCREENHEIGHT + 64);
    self.table.frame = self.view.frame;
    self.view.backgroundColor = [UIColor c2Color];
    self.table.backgroundColor = self.view.backgroundColor;

    UIImage *backButtonImage   = [UIImage imageNamed:@"wallet-back"];
    backButtonImage = [backButtonImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:backButtonImage style:UIBarButtonItemStylePlain target:self action:@selector(didTapBackButton)];
    self.navigationItem.leftBarButtonItem = backButton;
    
    UILabel* titlelable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, GSCALE(100) , GSCALE(44))];
    titlelable.font = [UIFont boldSystemFontOfSize:20];
    titlelable.textColor = [UIColor whiteColor];
    titlelable.textAlignment = NSTextAlignmentCenter;
//    titlelable.text = @"钱包";
    titlelable.text = NSLocalizedString(@"Wallet", nil);
    self.navigationItem.titleView = titlelable;
    
    imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, 0.688 * KSCREENWIDTH)];
    imgView.image = [UIImage imageNamed:@"图层-0"];
    imgView.userInteractionEnabled = YES;
    self.table.tableHeaderView = imgView;
    [self layoutImgView];
//    self.walletApi = [IGWalletApi new];
//    [self requestData];

}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.walletApi = [IGWalletApi new];
    [self requestData];
    self.navigationController.navigationBar.subviews[0] .alpha = 0;
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:28.0/255.0 green:172.0/255.0 blue:227.0/255.0 alpha:1.0];
    [self.navigationController.navigationBar setBackgroundImage:[self createImageWithColor:[UIColor colorWithRed:28.0/255.0 green:172.0/255.0 blue:227.0/255.0 alpha:1.0]] forBarMetrics:UIBarMetricsDefault];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
}

- (UIImage *)createImageWithColor:(UIColor *)color {
    
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.subviews[0].alpha = 1.0;
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:254.0/255.0 green:254.0/255.0 blue:254.0/255.0 alpha:1.0];
    [self.navigationController.navigationBar setBackgroundImage:[self createImageWithColor:[UIColor colorWithRed:254.0/255.0 green:254.0/255.0 blue:254.0/255.0 alpha:1.0]] forBarMetrics:UIBarMetricsDefault];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
    
}
- (void)didTapBackButton
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)layoutImgView
{
//余额
    UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame=CGRectMake(0, GSCALE(64) , KSCREENWIDTH/2, KSCREENWIDTH * 0.688 - GSCALE(64));
    btn.centerX=KSCREENWIDTH/2;
    [btn addTarget:self action:@selector(yee:) forControlEvents:UIControlEventTouchUpInside];
    [imgView addSubview:btn];
    
    UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"图层-2"]];
    img.frame = CGRectMake(btn.width/2 - 25, GSCALE(45), 50, 50);
    [btn addSubview:img];
    
    UILabel *msg = [[UILabel alloc] initWithFrame:CGRectMake(0, GSCALE(110), btn.width, 20)];
//    msg.text = @"余额";
    msg.text = NSLocalizedString(@"Balance", nil);
    msg.textColor = [UIColor whiteColor];
    msg.font = [UIFont systemFontOfSize:16];
    msg.textAlignment = NSTextAlignmentCenter;
    [btn addSubview:msg];
    
    yue = [[UILabel alloc] initWithFrame:CGRectMake(0, msg.bottom, btn.width, 40)];
    yue.textColor=[UIColor whiteColor];
    yue.textAlignment = NSTextAlignmentCenter;
    yue.font = [UIFont systemFontOfSize:20];
    yue.text=@"0.0";
    [btn addSubview:yue];
}

-(void)requestData{
    [self.walletApi startWithDecodeClass:[IGWalletResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        if (responseObject) {
            IGWalletResp *resp=responseObject;
            IGWalletModel *model=resp.data;
            yue.text= [NSString stringWithFormat:@"%.2f", [(model.balance.length>0 && model.balance.floatValue> 0)?model.balance:@"0" floatValue]];;
            yidian.text=[NSString stringWithFormat:@"%.1f", [(model.artPoint.length>0 && model.artPoint.floatValue > 0)?model.artPoint:@"0" floatValue]];
            [self.table reloadData];
        }
    }];
}

#pragma mark - UITableViewDelegate, UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0)
    {
        return 2;
    }
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    [tableView setSeparatorInset:UIEdgeInsetsMake(0, 20, 0, 0)];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    if (indexPath.section==0)
    {
        if (indexPath.row==0)
        {
//            cell.textLabel.text=@"交易记录";
            cell.textLabel.text=NSLocalizedString(@"Transaction Record", nil);
            
        }else if(indexPath.row==1)
        {
//            cell.textLabel.text=@"余额提现";
            cell.textLabel.text=NSLocalizedString(@"Balance Withdrawal",nil);
        }
    }
    else if(indexPath.section==1)
    {
        if (indexPath.row==0)
        {
//            cell.textLabel.text=@"常见问题";
            cell.textLabel.text=NSLocalizedString(@"FAQ",nil);
        }
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0)
    {
        if (indexPath.row==0)
        {
            [self billAction:nil];
        }
        else if (indexPath.row==1)
        {
            double money = [yue.text doubleValue];
            if (money < 100.0)
            {
//                [self presentMessageTips:@"余额不足100, 无法提现"];
                [self presentMessageTips:NSLocalizedString(@"Balance below RMB 100, Withdrawal unavailable", nil)];
                return;
            }
            
            if ([[IGUser getAuthens] integerValue] == 1)
            {
                //未认证
//                [self presentMessageTips:@"身份未认证"];
                [self presentMessageTips:NSLocalizedString(@"ID Unauthenticated", nil)];
            }
            else if([[IGUser getAuthens] integerValue] == 2)
            {
                //通过
                GetBalanceCashViewController *vc=[[GetBalanceCashViewController alloc]initWithNibName:@"GetBalanceCashViewController" bundle:nil];
                vc.money = yue.text;
                [UIViewController ig_pushViewController:vc animated:YES];
            }
            else
            {
                //是否认证（1未认证，2已认证，3待认证，3认证不通过）
                if ([[IGUser getAuthens] integerValue] == 3)
                {
                    //待审核
//                    [self presentMessageTips:@"身份认证待审核"];
                    [self presentMessageTips:NSLocalizedString(@"ID to be Verified", nil)];
                }
                else if([[IGUser getAuthens] integerValue]== 4)
                {
                    //未通过
//                    [self presentMessageTips:@"未通过身份认证"];
                    [self presentMessageTips:NSLocalizedString(@"ID Verification Failed", nil)];
                }
            }
        }
    }
    else if (indexPath.section==1)
    {
        if (indexPath.row==0)
        {
            IGWebViewController *vc = [[IGWebViewController alloc] initWithType:IGWebViewTypeSeven];
            [UIViewController ig_pushViewController:vc animated:YES];
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (void)billAction:(UIBarButtonItem *)btn
{
    [UIViewController ig_pushViewControllerClass:[BillViewController class] animated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section==1) {
        return 10.f;
    }
    return .01f;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return .01f;
}

-(void)yee:(UIButton *)btn
{
    BalanceViewController *vc=[[BalanceViewController alloc]init];
    vc.money = yue.text;
    [UIViewController ig_pushViewController:vc animated:YES];
    
}




@end
